<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 字段管理
 * @author LINK <link@shyohan.com>
 */
class FieldController extends QzController
{
    private $mid; //模型mid
    private $model; //模型缓存
    private $field; //字段缓存
    private $db; //模型

    /**
     * 构造函数
     */
    public function __init()
    {
        $this->mid = Q("mid", 0, "intval");
        $this->model = S("model");
        $this->field = S('field' . $this->mid);
        $this->db = K('Field');
    }

    /**
     * 字段列表
     */
    public function index()
    {
        $map['mid'] = array('IN', array($this->mid)); //分配当前模型的字段数据
        $field = M('field')->where($map)->order('fieldsort ASC')->all();
        $bread = make_bread(array('模型字段' => '', '字段列表' => ''));
        $this->assign('bread', $bread);
        $this->assign('field', $field);
        $this->assign('noallowdelete', FieldModel::$NoAllowDelete); //不允许删除字段
        $this->assign('noallowedit', FieldModel::$NoAllowEdit); //不允许删除字段
        $this->assign('noallowforbidden', FieldModel::$NoAllowForbidden); //不允许禁用字段
        $this->display();
    }

    /**
     * 更新字段排序
     */
    public function updateSort()
    {
        $orders = Q("fieldsort");
        foreach ($orders as $k => $v) {
            $this->db->save(array("fid" => $k, "fieldsort" => $v));
        }
        if ($this->db->updateCache()) {
            qz_ajax_return("排序成功");
        } else {
            qz_ajax_return_error('排序失败');
        }
    }

    /**
     * 添加字段
     */
    public function add()
    {
        if (IS_POST) {
            $Model = FieldModel::getInstance();
            if ($Model->addField()) {
                qz_ajax_return('添加字段成功');
            } else {
                qz_ajax_return_error($Model->error);
            }
        } else {
            $this->assign('model', $this->model[$this->mid]);
            $this->display();
        }
    }

    /**
     * 修改字段
     */
    public function edit()
    {
        if (IS_POST) {
            $Model = FieldModel::getInstance();
            if ($Model->editField()) {
                qz_ajax_return('修改成功');
            } else {
                qz_ajax_return_error($Model->error);
            }
        } else {
            $fid = Q('fid', 0, 'intval');
            $data = M('field')->where(array('fid' => $fid))->find();
            $field = isset($this->field[$data['field_name']]) ? $this->field[$data['field_name']] : null;
            $this->assign('field', $field);
            $setField = $this->fetch(CONTROLLER_VIEW_PATH . "Data/{$data['field_type']}/edit");
            $this->assign('setField', $setField);
            $this->assign('model_name', $this->model[$this->mid]['model_name']);
            $this->display();
        }
    }

    /**
     * 选择字段类型模板
     */
    public function getAddFieldTpl()
    {
        $action = Q("post.action"); //执行动作
        $field_type = Q("post.field_type"); //字段类型
        echo $this->fetch(CONTROLLER_VIEW_PATH . "Data/{$field_type}/{$action}");
        exit;
    }

    /**
     * 删除字段
     */
    public function del()
    {
        $fid = Q('id');
        if ($fid) {
            if ($this->db->delField()) {
                qz_ajax_return('字段删除成功');
            } else {
                qz_ajax_return_error($this->db->error);
            }
        } else {
            qz_ajax_return_error($this->db->error);
        }
    }

    /**
     * 更新字段缓存
     */
    public function updateCache()
    {
        if ($this->db->updateCache()) {
            qz_ajax_return('更新成功');
        } else {
            qz_ajax_return_error($this->db->error);
        }
    }

    /**
     * 禁用或开启字段
     */
    public function forbidden()
    {
        $status = Q('status', 0, 'intval');
        $fid = Q('fid', 0, 'intval');
        if (M('field')->save(array('fid' => $fid, 'status' => $status))) {
            $this->db->updateCache();
            qz_ajax_return('操作成功');
        } else {
            qz_ajax_return_error('操作失败');
        }
    }
}
